package com.thinkgem.jeesite.modules.euler.web;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.euler.entity.ChartDataEntity;
import com.thinkgem.jeesite.modules.euler.entity.SeriesEntity;
import com.thinkgem.jeesite.modules.euler.entity.TestEntity;
import com.thinkgem.jeesite.modules.euler.service.EulerTestService;

/**
 * @describe:
 * @author leo E-mail: zhangle231@163.com
 * @version 2015年4月1日 下午9:37:08
 */
@Controller
@RequestMapping(value = "${adminPath}/euler")
public class EulerController extends BaseController {

	@Autowired
	private EulerTestService eulerTestService;
	
	@ModelAttribute
	public TestEntity get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)) {
			return eulerTestService.get(id);
		}
		return new TestEntity();
	}

	@RequestMapping(value = { "list", "" })
	public String list(TestEntity testEntity, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		Page<TestEntity> page = eulerTestService.findPage(new Page<TestEntity>(
				request, response), testEntity);
		model.addAttribute("page", page);
		return "modules/euler/eulerList";
	}

	@RequestMapping(value = "form")
	public String form(TestEntity testEntity, Model model) {
		model.addAttribute("testEntity", testEntity);
		return "modules/euler/eulerForm";
	}
	
	@RequestMapping(value = "save")
	public String save(TestEntity testEntity, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, testEntity)){
			return form(testEntity, model);
		}
		eulerTestService.save(testEntity);
		addMessage(redirectAttributes, "保存数据'" + testEntity.getName() + "'成功");
		return "redirect:" + adminPath + "/euler/?repage";
	}
	
	@RequestMapping(value = "delete")
	public String delete(TestEntity testEntity, RedirectAttributes redirectAttributes) {
		eulerTestService.delete(testEntity);
		addMessage(redirectAttributes, "删除成功");
		return "redirect:" + adminPath + "/euler/?repage";
	}
	
	/**
	 * 修改方案，采用getChartData的方式，通过ajax来获取图表数据。这个方法已经不用了
	 * 但是保留页面跳转的功能。
	 * @param chartDataEntity
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "chart")
	public String chart(ChartDataEntity chartDataEntity, Model model) {
		model.addAttribute("chartDataEntity", chartDataEntity);
		return "modules/euler/eulerChart";
	}
	/**
	 * JSON 获取图表数据
	 * @return
	 */
	@RequestMapping(value = "getChartData")
	@ResponseBody
	public ChartDataEntity getChartData(String data) {
		ChartDataEntity chartDataEntity = new ChartDataEntity();
		chartDataEntity.setLegend(Arrays.asList("销量,利润".split(",")));
		chartDataEntity.setCategory(Arrays.asList("衬衫,羊毛衫,雪纺衫,裤子,高跟鞋,袜子".split(",")));
		List<SeriesEntity> series = new ArrayList<SeriesEntity>();
		for (String name : "销量,利润".split(",")) {
			SeriesEntity serie = new SeriesEntity();
			serie.setName(name);
			serie.setType("bar");
			List<Integer> seriesData = new ArrayList<Integer>();
			for (String i : data.split(",")) {
				seriesData.add(Integer.parseInt(i.trim()));
			}
			serie.setData(seriesData);
			series.add(serie);
		}
		chartDataEntity.setSeries(series );
		return chartDataEntity;
	}
	
	@RequestMapping(value = "chartMap") 
	public String chartMap() {
		return "modules/euler/eulerChartMap";
	}
	
	@RequestMapping(value = "configCharts")
	public String configCharts() {
		return "modules/euler/eulerConfigCharts";
	}
	
	@RequestMapping(value = "workFlow")
	public String workFlow() {
		return "modules/euler/eulerWorkFlow";
	}
}
